﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;

using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Threading;
using Excel = Microsoft.Office.Interop.Excel;
using System.Reflection;
using System.IO;

namespace INSAForManagers
{
    public partial class FormEEvalue : Form
    {
        private DataTable tbl = null;
        private int colIndex;
        private int rowIndex;
        private FormMain parent;

        public FormEEvalue(FormMain p)
        {
            InitializeComponent();
            parent = p;
            Control.CheckForIllegalCrossThreadCalls = false;
            tbl = new DataTable();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            FormMain.setMnuSEvalueEnabled();
            parent.stateRefresh();
            this.Close();
        }

        private void FormEEvalue_Load(object sender, EventArgs e)
        {
            try
            {
                addGridViewColumns();
                cboManager.SelectedIndex = 0;

                grdEvalue.Columns[2].Visible = false;
                grdEvalue.Columns[4].Frozen = true;         //틀고정
                grdEvalue.Columns[0].Width = 50;
                grdEvalue.Columns[1].Visible = false;
                grdEvalue.Columns[3].Width = 55;
                grdEvalue.Columns[4].Width = 70;
                grdEvalue.Columns[5].Width = 70;
                grdEvalue.Columns[6].Width = 70;
                grdEvalue.Columns[7].Width = 70;
                grdEvalue.Columns[8].Width = 70;
                grdEvalue.Columns[9].Width = 70;
                grdEvalue.Columns[10].Width = 70;
                grdEvalue.Columns[11].Width = 70;
                grdEvalue.Columns[12].Width = 70;
                grdEvalue.Columns[13].Width = 70;
                grdEvalue.Columns[14].Width = 70;
                grdEvalue.Columns[15].Width = 70;
                grdEvalue.Columns[0].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
                grdEvalue.Columns[1].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
                grdEvalue.Columns[2].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
                grdEvalue.Columns[3].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
                grdEvalue.Columns[5].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
                grdEvalue.Columns[4].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
                grdEvalue.Columns[6].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
                grdEvalue.Columns[7].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
                grdEvalue.Columns[8].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
                grdEvalue.Columns[9].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
                grdEvalue.Columns[10].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
                grdEvalue.Columns[11].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
                grdEvalue.Columns[12].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
                grdEvalue.Columns[13].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
                grdEvalue.Columns[11].DefaultCellStyle.BackColor = Color.LightGray;
                grdEvalue.Columns[12].DefaultCellStyle.BackColor = Color.LightGray;
                grdEvalue.Columns[13].DefaultCellStyle.BackColor = Color.LightGray;

                grdEvalue.Columns[4].DefaultCellStyle.Format = "#0.000";
                grdEvalue.Columns[5].DefaultCellStyle.Format = "#0.000";
                grdEvalue.Columns[6].DefaultCellStyle.Format = "#0.000";
                grdEvalue.Columns[7].DefaultCellStyle.Format = "#0.000";
                grdEvalue.Columns[8].DefaultCellStyle.Format = "#0.000";
                grdEvalue.Columns[9].DefaultCellStyle.Format = "#0.000";
                grdEvalue.Columns[10].DefaultCellStyle.Format = "#0.000";
                grdEvalue.Columns[11].DefaultCellStyle.Format = "#0.000";
                grdEvalue.Columns[12].DefaultCellStyle.Format = "#0.000";
                grdEvalue.Columns[13].DefaultCellStyle.Format = "#0.000";

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + "\n\n" + ex.StackTrace, "오류", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }

        private void cboManager_SelectedIndexChanged(object sender, EventArgs e)
        {

            cboManager.Enabled = false;

            changeManager();

            Thread.Sleep(500);

            cboManager.Enabled = true;
        }

        private void changeManager()
        {
            try
            {
                if (cboManager.SelectedIndex == 2)  //전체 선택
                {
                    btnUp.Enabled = false;
                    btnDown.Enabled = false;
                    btnAllot.Enabled = false;
                    btnModify.Enabled = false;
                    btnModify.Enabled = false;
                    btnRankCalc.Enabled = true;
                }
                else
                {
                    if ((int)getTargetType() > 0)
                    {
                        btnUp.Enabled = false;
                        btnDown.Enabled = false;
                        btnAllot.Enabled = false;
                        btnModify.Enabled = false;
                        btnRankCalc.Enabled = false;
                    }
                    else
                    {
                        btnUp.Enabled = true;
                        btnDown.Enabled = true;
                        btnAllot.Enabled = true;
                        btnModify.Enabled = true;
                        btnRankCalc.Enabled = false;
                    }
                }

                tbl.Rows.Clear();

                if (cboManager.SelectedIndex != 2)  //전체 선택이 아닌 경우
                {
                    lblAlert.Text = "[확인][별지 제4호 서식] 교사근무성적평정표를 출력하려면 여기서 인쇄합니다.";

                    ArrayList list = null;
                    if (DBUtil.isExistWorkEvalueInfo(getTargetType(), cboManager.SelectedIndex.ToString()))
                    {
                        list = DBUtil.getMTargetFromWorkEvalueTable(getTargetType(), cboManager.SelectedIndex.ToString());
                    }
                    else
                    {
                        list = DBUtil.getMTargetInfoList(getTargetType());
                    }

                    int nRow = 1;
                    foreach (ArrayList tmp in list)
                    {

                        DataRow row = tbl.NewRow();

                        row[0] = nRow;
                        row[1] = DBUtil.getOrganName(Config.Mvi.Organ);
                        row[2] = tmp[1];

                        //성명
                        if (DBUtil.getSameNameExist(tmp[2].ToString()))
                        {
                            row[3] = tmp[2].ToString() + "(" + tmp[4].ToString() + ")";
                        }
                        else
                        {
                            row[3] = tmp[2];
                        }
                        row[4] = DBUtil.getMultiControl(tmp[1].ToString());
                        row[5] = cboManager.SelectedIndex == 0 ? "평정자" : "확인자";

                        ArrayList data = DBUtil.getWorkEvalueInfo(cboManager.SelectedIndex.ToString(), tmp[1].ToString());
                        if (data.Count > 0)
                        {
                            row[6] = data[0];
                            row[7] = data[1];
                            row[8] = data[2];
                            row[9] = data[3];
                            row[10] = data[4];
                            row[11] = data[5];
                            row[12] = data[6];
                        }
                        else
                        {
                            row[6] = 0.0;
                            row[7] = 0.0;
                            row[8] = 0.0;
                            row[9] = 0.0;
                            row[10] = 0.0;
                            row[11] = 0.0;
                            row[12] = 0.0;
                        }

                        tbl.Rows.Add(row);

                        nRow++;
                    }

                    grdEvalue.DataSource = tbl;


                    grdEvalue.Columns[0].Visible = true;
                    grdEvalue.Columns[13].Visible = false;
                    grdEvalue.Columns[14].Visible = false;
                    grdEvalue.Columns[15].Visible = false;

                    if ((int)getTargetType() > 0)
                    {
                        grdEvalue.EditMode = DataGridViewEditMode.EditOnEnter;
                        grdEvalue.SelectionMode = DataGridViewSelectionMode.CellSelect;
                        grdEvalue.Columns[0].ReadOnly = true;
                        grdEvalue.Columns[1].ReadOnly = true;
                        grdEvalue.Columns[3].ReadOnly = true;
                        grdEvalue.Columns[4].ReadOnly = true;
                        grdEvalue.Columns[5].ReadOnly = true;
                        grdEvalue.Columns[11].ReadOnly = true;
                        grdEvalue.Columns[12].ReadOnly = true;

                        grdEvalue.Columns[6].DefaultCellStyle.BackColor = Color.White;
                        grdEvalue.Columns[7].DefaultCellStyle.BackColor = Color.White;
                        grdEvalue.Columns[8].DefaultCellStyle.BackColor = Color.White;
                        grdEvalue.Columns[9].DefaultCellStyle.BackColor = Color.White;
                        grdEvalue.Columns[10].DefaultCellStyle.BackColor = Color.White;
                    }
                    else
                    {
                        grdEvalue.EditMode = DataGridViewEditMode.EditProgrammatically;
                        grdEvalue.SelectionMode = DataGridViewSelectionMode.FullRowSelect;

                        grdEvalue.Columns[6].DefaultCellStyle.BackColor = Color.FromArgb(240, 240, 240);
                        grdEvalue.Columns[7].DefaultCellStyle.BackColor = Color.FromArgb(240, 240, 240);
                        grdEvalue.Columns[8].DefaultCellStyle.BackColor = Color.FromArgb(240, 240, 240);
                        grdEvalue.Columns[9].DefaultCellStyle.BackColor = Color.FromArgb(240, 240, 240);
                        grdEvalue.Columns[10].DefaultCellStyle.BackColor = Color.FromArgb(240, 240, 240);
                    }

                }
                else //전체 선택인 경우
                {
                    lblAlert.Text = "[확인]이 기능은 근무성적평정에 참고용으로만 제공되는 것입니다.";

                    ArrayList list = null;
                    if (DBUtil.isExistWorkControlInfo())
                    {
                        list = DBUtil.getMTargetArrayFromWorkControlTable(getTargetType());
                    }
                    if (list == null || list.Count == 0)
                    {
                        list = DBUtil.getMTargetInfoList(getTargetType());
                    }
                    int nRow = 1;
                    foreach (ArrayList tmp in list)
                    {
                        DataRow row = tbl.NewRow();

                        row[0] = nRow;//연번
                        row[1] = DBUtil.getOrganName(Config.Mvi.Organ);//기관명
                        row[2] = tmp[1];//개인번호(숨김)
                        //성명
                        if (DBUtil.getSameNameExist(tmp[1].ToString()))
                        {
                            row[3] = tmp[2].ToString() + "(" + tmp[4].ToString() + ")";
                        }
                        else
                        {
                            row[3] = tmp[2];
                        }
                        row[4] = DBUtil.getMultiControl(tmp[1].ToString());
                        row[5] = "평정자";

                        //평정 정보
                        ArrayList data = DBUtil.getWorkEvalueInfo("0", tmp[1].ToString());
                        if (data.Count > 0)
                        {
                            row[6] = data[0];
                            row[7] = data[1];
                            row[8] = data[2];
                            row[9] = data[3];
                            row[10] = data[4];
                            row[11] = data[5];
                            row[12] = data[6];
                        }
                        else
                        {
                            row[6] = 0.0;
                            row[7] = 0.0;
                            row[8] = 0.0;
                            row[9] = 0.0;
                            row[10] = 0.0;
                            row[11] = 0.0;
                            row[12] = 0.0;
                        }

                        //총점, 순위
                        data = DBUtil.getWorkControlInfo(tmp[1].ToString());
                        if (data.Count > 0)
                        {
                            row[13] = data[0];
                            row[14] = Double.Parse(data[1].ToString()).ToString("#0.000");
                            row[15] = data[2];
                        }


                        tbl.Rows.Add(row);


                        row = tbl.NewRow();

                        row[1] = "";
                        row[2] = tmp[1];
                        row[3] = "";
                        row[4] = 0.0;
                        row[5] = "확인자";

                        data = DBUtil.getWorkEvalueInfo("1", tmp[1].ToString());
                        if (data.Count > 0)
                        {
                            row[6] = data[0];
                            row[7] = data[1];
                            row[8] = data[2];
                            row[9] = data[3];
                            row[10] = data[4];
                            row[11] = data[5];
                            row[12] = data[6];
                        }
                        else
                        {
                            row[6] = 0.0;
                            row[7] = 0.0;
                            row[8] = 0.0;
                            row[9] = 0.0;
                            row[10] = 0.0;
                            row[11] = 0.0;
                            row[12] = 0.0;
                        }

                        tbl.Rows.Add(row);

                        nRow++;
                    }

                    grdEvalue.DataSource = tbl;

                    //컬럼 헤더 소팅 방지
                    for (int i = 0; i < grdEvalue.Columns.Count; i++)
                    {
                        grdEvalue.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable;
                    }

                    //격줄로 배경색 지정
                    for (int i = 0; i < grdEvalue.Rows.Count - 1; i++)
                    {
                        if (i % 2 == 1)
                        {
                            grdEvalue.Rows[i].DefaultCellStyle.BackColor = Color.LightGray;
                        }
                        else
                        {
                            grdEvalue.Rows[i].DefaultCellStyle.BackColor = Color.FromArgb(240, 240, 240);
                        }
                    }

                    grdEvalue.Columns[0].Visible = false;
                    grdEvalue.Columns[13].Visible = true;
                    grdEvalue.Columns[14].Visible = true;
                    grdEvalue.Columns[15].Visible = true;
                    grdEvalue.Columns[14].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
                    grdEvalue.Columns[15].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;

                    grdEvalue.EditMode = DataGridViewEditMode.EditProgrammatically;
                    grdEvalue.SelectionMode = DataGridViewSelectionMode.FullRowSelect;

                    grdEvalue.Columns[6].DefaultCellStyle.BackColor = Color.LightGray;
                    grdEvalue.Columns[7].DefaultCellStyle.BackColor = Color.LightGray;
                    grdEvalue.Columns[8].DefaultCellStyle.BackColor = Color.LightGray;
                    grdEvalue.Columns[9].DefaultCellStyle.BackColor = Color.LightGray;
                    grdEvalue.Columns[10].DefaultCellStyle.BackColor = Color.LightGray;
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + "\n\n" + ex.StackTrace, "오류", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void addGridViewColumns()
        {
            DataColumn col;

            int valuer_num = Config.ValuerList.Count;

            //연번
            col = new DataColumn();
            col.DataType = Type.GetType("System.Int32");
            col.ColumnName = "연번";
            col.AllowDBNull = true;
            col.ReadOnly = false;
            tbl.Columns.Add(col);

            //소속
            col = new DataColumn();
            col.DataType = Type.GetType("System.String");
            col.ColumnName = "소속";
            col.AllowDBNull = true;
            col.ReadOnly = false;
            tbl.Columns.Add(col);

            //개인번호
            col = new DataColumn();
            col.DataType = Type.GetType("System.String");
            col.ColumnName = "개인번호";
            col.AllowDBNull = true;
            col.ReadOnly = false;
            tbl.Columns.Add(col);

            //평가자 대상자 성명
            col = new DataColumn();
            col.DataType = Type.GetType("System.String");
            col.ColumnName = "성명";
            col.AllowDBNull = true;
            col.ReadOnly = false;
            tbl.Columns.Add(col);

            //다면평가점
            col = new DataColumn();
            col.DataType = Type.GetType("System.Double");
            col.ColumnName = "다면평가점(30)";
            col.AllowDBNull = true;
            col.ReadOnly = false;
            tbl.Columns.Add(col);

            //평가자
            col = new DataColumn();
            col.DataType = Type.GetType("System.String");
            col.ColumnName = "평가자";
            col.AllowDBNull = true;
            col.ReadOnly = false;
            tbl.Columns.Add(col);


            //교육자로서의 품성
            col = new DataColumn();
            col.DataType = Type.GetType("System.Double");
            col.ColumnName = "교육자로서의 품성(10)";
            col.AllowDBNull = true;
            col.ReadOnly = false;
            tbl.Columns.Add(col);

            //공직자로서의 자세
            col = new DataColumn();
            col.DataType = Type.GetType("System.Double");
            col.ColumnName = "공직자로서의 자세(10)";
            col.AllowDBNull = true;
            col.ReadOnly = false;
            tbl.Columns.Add(col);

            //학습지도
            col = new DataColumn();
            col.DataType = Type.GetType("System.Double");
            col.ColumnName = "학습지도(40)";
            col.AllowDBNull = true;
            col.ReadOnly = false;
            tbl.Columns.Add(col);

            //생활지도
            col = new DataColumn();
            col.DataType = Type.GetType("System.Double");
            col.ColumnName = "생활지도(20)";
            col.AllowDBNull = true;
            col.ReadOnly = false;
            tbl.Columns.Add(col);

            //교육연구 및 담당업무
            col = new DataColumn();
            col.DataType = Type.GetType("System.Double");
            col.ColumnName = "교육연구 및 담당업무(20)";
            col.AllowDBNull = true;
            col.ReadOnly = false;
            tbl.Columns.Add(col);


            //평가점
            col = new DataColumn();
            col.DataType = Type.GetType("System.Double");
            col.ColumnName = "평정점";
            col.AllowDBNull = true;
            col.ReadOnly = false;
            tbl.Columns.Add(col);

            //환산점
            col = new DataColumn();
            col.DataType = Type.GetType("System.Double");
            col.ColumnName = "환산점";
            col.AllowDBNull = true;
            col.ReadOnly = false;
            tbl.Columns.Add(col);

            //총점
            col = new DataColumn();
            col.DataType = Type.GetType("System.Double");
            col.ColumnName = "총점";
            col.AllowDBNull = true;
            col.ReadOnly = false;
            tbl.Columns.Add(col);

            //조정점
            col = new DataColumn();
            col.DataType = Type.GetType("System.Double");
            col.ColumnName = "조정점";
            col.AllowDBNull = true;
            col.ReadOnly = false;
            tbl.Columns.Add(col);

            //순위
            col = new DataColumn();
            col.DataType = Type.GetType("System.Int32");
            col.ColumnName = "순위";
            col.AllowDBNull = true;
            col.ReadOnly = false;
            tbl.Columns.Add(col);


            grdEvalue.DataSource = tbl;


        }

        private void btnUp_Click(object sender, EventArgs e)
        {
            try
            {
                if (grdEvalue.SelectedRows == null || grdEvalue.SelectedRows.Count == 0)
                {
                    MessageBox.Show("순위를 변경할 열을 먼저 선택하세요!    ", "확인");
                    return;
                }

                int step = 1;

                if (chkMoveStep.Checked)
                {
                    if (rowIndex <= 4)
                    {
                        MessageBox.Show("5행 위로 이동할 수 없습니다.   ", "확인");
                        return;
                    }

                    step = 5;
                }

                if (rowIndex <= 0)
                {
                    MessageBox.Show("최상위 입니다.    ", "확인");
                    return;
                }

                /*
                 * 5행씩 이동할 경우 기존에는 교체 이동이였으나 현재는 순차적으로 이동하도록 수정함
                 * 2010-10-29 수정
                 */
                for (int i = 0; i < step; i++)
                {

                    DataGridViewRow currRow = grdEvalue.Rows[rowIndex];
                    ArrayList cRow = new ArrayList();
                    cRow.Add(((DataRowView)currRow.DataBoundItem).Row[2]);
                    cRow.Add(((DataRowView)currRow.DataBoundItem).Row[3]);
                    cRow.Add(((DataRowView)currRow.DataBoundItem).Row[4]);

                    DataGridViewRow nextRow = grdEvalue.Rows[rowIndex - 1];
                    ArrayList nRow = new ArrayList();
                    nRow.Add(((DataRowView)nextRow.DataBoundItem).Row[2]);
                    nRow.Add(((DataRowView)nextRow.DataBoundItem).Row[3]);
                    nRow.Add(((DataRowView)nextRow.DataBoundItem).Row[4]);

                    grdEvalue.Rows[rowIndex].Cells[2].Value = nRow[0];
                    grdEvalue.Rows[rowIndex].Cells[3].Value = nRow[1];
                    grdEvalue.Rows[rowIndex].Cells[4].Value = nRow[2];

                    grdEvalue.Rows[rowIndex - 1].Cells[2].Value = cRow[0];
                    grdEvalue.Rows[rowIndex - 1].Cells[3].Value = cRow[1];
                    grdEvalue.Rows[rowIndex - 1].Cells[4].Value = cRow[2];

                    rowIndex--;

                }

                //rowIndex = rowIndex - step;

                grdEvalue.Rows[rowIndex].Selected = true;

                grdEvalue.FirstDisplayedScrollingRowIndex = rowIndex;

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + "\n\n" + ex.StackTrace, "오류", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }

        private void grdEvalue_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            rowIndex = e.RowIndex;
            colIndex = e.ColumnIndex;
        }

        private void btnDown_Click(object sender, EventArgs e)
        {
            try
            {
                if (grdEvalue.SelectedRows == null || grdEvalue.SelectedRows.Count == 0)
                {
                    MessageBox.Show("순위를 변경할 열을 먼저 선택하세요!    ", "확인");
                    return;
                }

                int step = 1;

                if (chkMoveStep.Checked)
                {
                    if (rowIndex >= grdEvalue.Rows.Count - 5)
                    {
                        MessageBox.Show("5행 아래로 이동할 수 없습니다.   ", "확인");
                        return;
                    }

                    step = 5;
                }

                if (rowIndex == grdEvalue.Rows.Count - 1)
                {
                    MessageBox.Show("최하위 입니다.    ", "확인");
                    return;
                }

                /*
                 * 5행씩 이동할 경우 기존에는 교체 이동이였으나 현재는 순차적으로 이동하도록 수정함
                 * 2010-10-29 수정
                 */
                for (int i = 0; i < step; i++)
                {

                    DataGridViewRow currRow = grdEvalue.Rows[rowIndex];
                    ArrayList cRow = new ArrayList();
                    cRow.Add(((DataRowView)currRow.DataBoundItem).Row[2]);
                    cRow.Add(((DataRowView)currRow.DataBoundItem).Row[3]);
                    cRow.Add(((DataRowView)currRow.DataBoundItem).Row[4]);

                    DataGridViewRow nextRow = grdEvalue.Rows[rowIndex + 1];
                    ArrayList nRow = new ArrayList();
                    nRow.Add(((DataRowView)nextRow.DataBoundItem).Row[2]);
                    nRow.Add(((DataRowView)nextRow.DataBoundItem).Row[3]);
                    nRow.Add(((DataRowView)nextRow.DataBoundItem).Row[4]);

                    grdEvalue.Rows[rowIndex].Cells[2].Value = nRow[0];
                    grdEvalue.Rows[rowIndex].Cells[3].Value = nRow[1];
                    grdEvalue.Rows[rowIndex].Cells[4].Value = nRow[2];

                    grdEvalue.Rows[rowIndex + 1].Cells[2].Value = cRow[0];
                    grdEvalue.Rows[rowIndex + 1].Cells[3].Value = cRow[1];
                    grdEvalue.Rows[rowIndex + 1].Cells[4].Value = cRow[2];

                    rowIndex++;

                }

                //rowIndex = rowIndex + step;

                grdEvalue.Rows[rowIndex].Selected = true;

                grdEvalue.FirstDisplayedScrollingRowIndex = rowIndex;

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + "\n\n" + ex.StackTrace, "오류", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void item_Click(object sender, EventArgs e)
        {
            Form f = new FormEEvalueElement(grdEvalue.CurrentRow);
            f.ShowDialog(this);
        }

        private void grdEvalue_MouseClick(object sender, MouseEventArgs e)
        {

            if (cboManager.SelectedIndex != 2)
            {

                if (rowIndex >= 0 && rowIndex <= grdEvalue.Rows.Count - 2)
                {

                    if (e.Button == MouseButtons.Right)
                    {
                        ContextMenu cm = new ContextMenu();
                        MenuItem item = new MenuItem("조정");
                        item.Click += new EventHandler(item_Click);
                        cm.MenuItems.Add(item);

                        grdEvalue.ContextMenu = cm;

                        grdEvalue.ContextMenu.Show((DataGridView)sender, new Point(e.X, e.Y));
                    }

                }
            }

        }

        private void btnAllot_Click(object sender, EventArgs e)
        {
            btnAllot.Enabled = false;


            if (grdEvalue.Rows.Count < 2)
            {
                btnAllot.Enabled = true;
                return;
            }

            Thread t = new Thread(new ThreadStart(AllotProcess));
            t.Start();


        }

        private void AllotProcess()
        {
            /*
             * 기본전제 : 현재 목록은 근무평정점수가 높게 배정될 순서로 나열되어 있음
             * */

            /*
             * 현재 GridView에 있는 대상자 기준
             * 전체 평정 인원, 수 평정 대상자수, 우 평정 대상자수, 미-양 평정 대상자수 구하고
             * 각 평어 별 배점을 순서대로 배당
             * 요소별 점수 자동 배당
             * 환산점 자동 계산
             * */

            try
            {

                ArrayList num = DBUtil.getRatioNum(getTargetType());

                Int16 su_num = (Int16)num[0];
                Int16 wo_num = (Int16)num[1];
                Int16 mi_num = (Int16)((Int16)num[2] + (Int16)num[3]);



                ArrayList su_array = DBUtil.getTotalSu(su_num);
                ArrayList wo_array = DBUtil.getTotalWo(wo_num);
                ArrayList mi_array = DBUtil.getTotalMi(mi_num);

                double ratio = cboManager.SelectedIndex == 0 ? 0.3 : 0.4;

                Random rnd = new Random();
                Random rnd1 = new Random();
                int chk;
                for (int i = 0; i < su_array.Count; i++)
                {
                    chk = rnd1.Next(100);

                    double tmp = Double.Parse(su_array[i].ToString()) + (chk % 2 == 0 ? Util.getRandomRange(su_num) * rnd.NextDouble() : -1 * Util.getRandomRange(su_num) * rnd.NextDouble());

                    if (tmp > 100)
                    {
                        tmp = 100.00;
                    }
                    else if (tmp < 95)
                    {
                        tmp = 95.0;
                    }

                    //요소별 랜덤 작성
                    double dRandom1 = rnd.NextDouble();
                    double item1 = tmp * 0.1;
                    int chkItem1 = rnd.Next(100);
                    item1 += (chkItem1 % 2 == 0 ? dRandom1 * 0.05 : (-1) * dRandom1 * 0.05);
                    if (item1 > 10.00) item1 = 10.00;

                    double dRandom2 = rnd.NextDouble();
                    double item2 = tmp * 0.1;
                    int chkItem2 = rnd.Next(100);
                    item2 += (chkItem2 % 2 == 0 ? dRandom2 * 0.05 : (-1) * dRandom2 * 0.05);
                    if (item2 > 10.00) item2 = 10.00;

                    double dRandom3 = rnd.NextDouble();
                    double item5 = tmp * 0.2;
                    int chkItem3 = rnd.Next(100);
                    item5 += (chkItem3 % 2 == 0 ? dRandom3 * 0.1 : (-1) * dRandom3 * 0.1);
                    if (item5 > 20.00) item5 = 20.00;

                    double dRandom4 = rnd.NextDouble();
                    double item4 = tmp * 0.2;
                    int chkItem4 = rnd.Next(100);
                    item4 += (chkItem4 % 2 == 0 ? dRandom4 * 0.1 : (-1) * dRandom4 * 0.1);
                    if (item4 > 20.00) item4 = 20.00;

                    double item3 = tmp - (item1 + item2 + item5 + item4);
                    if (item3 > 40.00)
                    {
                        tmp = tmp - (item3 - 40.00);
                        item3 = 40.00;
                    }

                    grdEvalue.Rows[i].Cells[6].Value = item1;
                    grdEvalue.Rows[i].Cells[7].Value = item2;
                    grdEvalue.Rows[i].Cells[8].Value = item3;
                    grdEvalue.Rows[i].Cells[9].Value = item4;
                    grdEvalue.Rows[i].Cells[10].Value = item5;
                    grdEvalue.Rows[i].Cells[11].Value = tmp;
                    grdEvalue.Rows[i].Cells[12].Value = tmp * ratio;

                }

                for (int i = su_array.Count; i < (su_array.Count + wo_array.Count); i++)
                {
                    chk = rnd1.Next(100);
                    double tmp = Double.Parse(wo_array[i - su_array.Count].ToString()) + (chk % 2 == 0 ? Util.getRandomRange(wo_num) * rnd.NextDouble() : -1 * Util.getRandomRange(wo_num) * rnd.NextDouble());
                    if (tmp > 94.9)
                    {
                        tmp = 94.9;
                    }
                    else if (tmp < 90)
                    {
                        tmp = 90.0;
                    }

                    //요소별 랜덤 작성
                    double dRandom1 = rnd.NextDouble();
                    double item1 = tmp * 0.1;
                    int chkItem1 = rnd.Next(100);
                    item1 += (chkItem1 % 2 == 0 ? dRandom1 * 0.05 : (-1) * dRandom1 * 0.05);
                    if (item1 > 10.00) item1 = 10.00;

                    double dRandom2 = rnd.NextDouble();
                    double item2 = tmp * 0.1;
                    int chkItem2 = rnd.Next(100);
                    item2 += (chkItem2 % 2 == 0 ? dRandom2 * 0.05 : (-1) * dRandom2 * 0.05);
                    if (item2 > 10.00) item2 = 10.00;

                    double dRandom3 = rnd.NextDouble();
                    double item5 = tmp * 0.2;
                    int chkItem3 = rnd.Next(100);
                    item5 += (chkItem3 % 2 == 0 ? dRandom3 * 0.1 : (-1) * dRandom3 * 0.1);
                    if (item5 > 20.00) item5 = 20.00;

                    double dRandom4 = rnd.NextDouble();
                    double item4 = tmp * 0.2;
                    int chkItem4 = rnd.Next(100);
                    item4 += (chkItem4 % 2 == 0 ? dRandom4 * 0.1 : (-1) * dRandom4 * 0.1);
                    if (item4 > 20.00) item4 = 20.00;

                    double item3 = tmp - (item1 + item2 + item5 + item4);

                    grdEvalue.Rows[i].Cells[6].Value = item1;
                    grdEvalue.Rows[i].Cells[7].Value = item2;
                    grdEvalue.Rows[i].Cells[8].Value = item3;
                    grdEvalue.Rows[i].Cells[9].Value = item4;
                    grdEvalue.Rows[i].Cells[10].Value = item5;
                    grdEvalue.Rows[i].Cells[11].Value = tmp;
                    grdEvalue.Rows[i].Cells[12].Value = tmp * ratio;
                }

                for (int i = (su_array.Count + wo_array.Count); i < (su_array.Count + wo_array.Count + mi_array.Count); i++)
                {
                    chk = rnd1.Next(100);

                    double tmp = Double.Parse(mi_array[i - su_array.Count - wo_array.Count].ToString()) + (chk % 2 == 0 ? Util.getRandomRange(mi_num) * rnd.NextDouble() : -1 * Util.getRandomRange(wo_num) * rnd.NextDouble());
                    if (tmp > 89.9)
                    {
                        tmp = 89.9;
                    }
                    else if (tmp < 85)
                    {
                        tmp = 85.0;
                    }

                    //요소별 랜덤 작성
                    double dRandom1 = rnd.NextDouble();
                    double item1 = tmp * 0.1;
                    int chkItem1 = rnd.Next(100);
                    item1 += (chkItem1 % 2 == 0 ? dRandom1 * 0.05 : (-1) * dRandom1 * 0.05);
                    if (item1 > 10.00) item1 = 10.00;

                    double dRandom2 = rnd.NextDouble();
                    double item2 = tmp * 0.1;
                    int chkItem2 = rnd.Next(100);
                    item2 += (chkItem2 % 2 == 0 ? dRandom2 * 0.05 : (-1) * dRandom2 * 0.05);
                    if (item2 > 10.00) item2 = 10.00;

                    double dRandom3 = rnd.NextDouble();
                    double item5 = tmp * 0.2;
                    int chkItem3 = rnd.Next(100);
                    item5 += (chkItem3 % 2 == 0 ? dRandom3 * 0.10 : (-1) * dRandom3 * 0.10);
                    if (item5 > 20.00) item5 = 20.00;

                    double dRandom4 = rnd.NextDouble();
                    double item4 = tmp * 0.2;
                    int chkItem4 = rnd.Next(100);
                    item4 += (chkItem4 % 2 == 0 ? dRandom4 * 0.1 : (-1) * dRandom4 * 0.1);
                    if (item4 > 20.00) item4 = 20.00;

                    double item3 = tmp - (item1 + item2 + item5 + item4);


                    grdEvalue.Rows[i].Cells[6].Value = item1;
                    grdEvalue.Rows[i].Cells[7].Value = item2;
                    grdEvalue.Rows[i].Cells[8].Value = item3;
                    grdEvalue.Rows[i].Cells[9].Value = item4;
                    grdEvalue.Rows[i].Cells[10].Value = item5;
                    grdEvalue.Rows[i].Cells[11].Value = tmp;
                    grdEvalue.Rows[i].Cells[12].Value = tmp * ratio;
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + "\n\n" + ex.StackTrace, "오류", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            btnAllot.Enabled = true;
            btnAllot.Focus();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (grdEvalue.Rows[0].Cells[6].Value.ToString().Equals("") == true)
                {
                    MessageBox.Show("배점이 없습니다.!    ", "확인", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }

                if (cboManager.SelectedIndex != 2)
                {
                    for (int i = 0; i < DBUtil.getMTargetCount(getTargetType()); i++)
                    {

                        ArrayList list = new ArrayList();
                        list.Add(cboManager.SelectedIndex);
                        list.Add(grdEvalue.Rows[i].Cells[2].Value);
                        list.Add(grdEvalue.Rows[i].Cells[6].Value);
                        list.Add(grdEvalue.Rows[i].Cells[7].Value);
                        list.Add(grdEvalue.Rows[i].Cells[8].Value);
                        list.Add(grdEvalue.Rows[i].Cells[9].Value);
                        list.Add(grdEvalue.Rows[i].Cells[10].Value);
                        list.Add(grdEvalue.Rows[i].Cells[11].Value);
                        list.Add(grdEvalue.Rows[i].Cells[12].Value);
                        DateTime now = DateTime.Now;
                        list.Add(now);

                        DBUtil.writeWorkValue(list);
                    }
                }
                else
                {
                    if (grdEvalue.Rows[0].Cells[10].Value.ToString().Equals("") || grdEvalue.Rows[0].Cells[11].Value.ToString().Equals("") == true || grdEvalue.Rows[0].Cells[12].Value.ToString().Equals("") == true)
                    {
                        MessageBox.Show("조정 버튼을 먼저 클릭하세요!    ", "확인", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }

                    int index;
                    for (int i = 0; i < DBUtil.getMTargetCount(getTargetType()); i++)
                    {
                        index = i * 2;
                        ArrayList list = new ArrayList();
                        list.Add(grdEvalue.Rows[index].Cells[2].Value);
                        list.Add(grdEvalue.Rows[index].Cells[13].Value);
                        list.Add(grdEvalue.Rows[index].Cells[14].Value);
                        list.Add(grdEvalue.Rows[index].Cells[15].Value);
                        DateTime now = DateTime.Now;
                        list.Add(now);

                        DBUtil.writeWorkControl(list);
                    }
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "오류", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            MessageBox.Show("저장하였습니다.    ", "확인", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }


        private void btnRankCalc_Click_1(object sender, EventArgs e)
        {
            Thread t = new Thread(new ThreadStart(calcRanking));
            t.Start();
        }

        private void btnModify_Click(object sender, EventArgs e)
        {
            if (grdEvalue.Rows.Count > 1 && grdEvalue.CurrentRow.Cells[7].Value.ToString().Equals("") == false)
            {
                if (grdEvalue.CurrentRow.Index > -1 && grdEvalue.CurrentRow.Index < DBUtil.getMTargetCount(getTargetType()))
                {
                    FormEEvalueElement f = new FormEEvalueElement(grdEvalue.CurrentRow);
                    f.ShowDialog(this);
                }
                else
                {
                    MessageBox.Show("대상자를 먼저 선택하세요!    ", "확인");
                }
            }
            else
            {
                MessageBox.Show("배점을 먼저 하세요!    ", "확인");
            }
        }

        private void calcRanking()
        {
            try
            {
                grdEvalue.FirstDisplayedScrollingColumnIndex = 10;

                if (cboManager.SelectedIndex != 2)
                {
                    MessageBox.Show("항목 중에서 전체를 선택한 후 등위 계산을 실행하십시오!   ", "확인");
                    cboManager.Focus();
                    return;
                }

                //평정자 확인자 모두 배점을 해야만 함
                if (DBUtil.getWorkEvaluerCount() < 2)
                {
                    MessageBox.Show("평정자와 확인자 모두 근무 성적 평정 후 계산을 하십시오.    ", "확인");
                    return;
                }

                int targetCount = DBUtil.getMTargetCount(getTargetType());

                double[] total = new double[targetCount];

                //총점
                for (int i = 0; i < targetCount; i++)
                {
                    int index = 2 * i;
                    total[i] = Double.Parse(grdEvalue.Rows[index].Cells[12].Value.ToString()) + Double.Parse(grdEvalue.Rows[index + 1].Cells[12].Value.ToString());
                    grdEvalue.Rows[index].Cells[13].Value = Double.Parse(total[i].ToString()).ToString("#0.000");
                }

                //동점 검사
                int[] inx = Util.existedSamePoint(total);
                if (inx[0] != 0)
                {
                    MessageBox.Show("총점(" + total[inx[0]] + ")에 동점이 존재합니다.    \n\n대상자들의 순위를 재조정하십시오.    ", "확인", MessageBoxButtons.OK, MessageBoxIcon.Warning);

                    return;
                }

                //순위
                for (int i = 0; i < DBUtil.getMTargetCount(getTargetType()); i++)
                {
                    int index = 2 * i;
                    grdEvalue.Rows[index].Cells[15].Value = Util.getSortNum(total, total[i]);
                }

                //조정점
                ArrayList num = DBUtil.getRatioNum(getTargetType());
                int su_num = (Int16)num[0];
                int wo_num = (Int16)num[1];
                int mi_num = (Int16)num[2] + (Int16)num[3];

                int chk = 1;//평어별 인원 카운트
                string lang = "수";
                int chknum = su_num;
                for (int i = 1; i <= DBUtil.getMTargetCount(getTargetType()); i++)
                {
                    if (i == su_num + 1)
                    {
                        lang = "우";
                        chk = 1;
                        chknum = wo_num;
                    }
                    else if (i == su_num + wo_num + 1)
                    {
                        lang = "미";
                        chk = 1;
                        chknum = mi_num;
                    }

                    for (int j = 0; j < DBUtil.getMTargetCount(getTargetType()); j++)
                    {
                        int index = j * 2;
                        if (grdEvalue.Rows[index].Cells[15].Value.ToString().Equals(i.ToString()) == true)
                        {
                            grdEvalue.Rows[index].Cells[14].Value = DBUtil.getWorkControlPoint(lang, chk, chknum).ToString("#0.000");
                        }
                    }

                    chk++;
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + "\n\n" + ex.StackTrace, "오류", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            btnPrint.Enabled = false;

            if (cboManager.SelectedIndex != 2)
            {
                Thread t = new Thread(new ThreadStart(printXls));
                t.Start();
            }
            else
            {
                Thread t = new Thread(new ThreadStart(printXlsFinal));
                t.Start();
            }
        }

        private void printXls()
        {
            try
            {


                lblMsg.Text = "인쇄 준비 중...";

                Util.openXls(Application.StartupPath + "\\WorkEvalue");

                //평정기간설정
                Excel.Range range = Util.OSheet.get_Range("A3", Missing.Value);
                range.set_Value(Missing.Value, Config.Mvi.SDate);

                range = Util.OSheet.get_Range("A4", Missing.Value);
                range.set_Value(Missing.Value, Config.Mvi.EDate);

                //확인자
                range = Util.OSheet.get_Range("E3", Missing.Value);
                range.set_Value(Missing.Value, "교장");

                range = Util.OSheet.get_Range("E4", Missing.Value);
                range.set_Value(Missing.Value, Config.Mvi.Confirmer);

                //평정자
                range = Util.OSheet.get_Range("I3", Missing.Value);
                range.set_Value(Missing.Value, "교감");

                range = Util.OSheet.get_Range("I4", Missing.Value);
                range.set_Value(Missing.Value, Config.Mvi.Evaluer1 + "," + Config.Mvi.Evaluer2);

                lblMsg.Text = "데이터 입력 중...";

                //근무성적평정대상자 목록

                //ArrayList list = DBUtil.getMTargetList();
                ArrayList list = DBUtil.getMTargetArrayFromWorkControlTable(getTargetType());

                int rowIndex = 16;//2행씩 증가
                for (int i = 0; i < list.Count; i++)
                {
                    ArrayList tmp = (ArrayList)list[i];

                    double total = 0;

                    //소속
                    range = Util.OSheet.get_Range("A" + (rowIndex).ToString(), Missing.Value);
                    range.set_Value(Missing.Value, DBUtil.getOrganName(Config.Mvi.Organ));

                    //성명
                    range = Util.OSheet.get_Range("B" + (rowIndex).ToString(), Missing.Value);
                    string name = DBUtil.getMTargetName(tmp[1].ToString());
                    if (DBUtil.getSameNameExist(name)) name = name + "(" + DBUtil.getMTargetInfo(tmp[1].ToString())[2].ToString() + ")";
                    range.set_Value(Missing.Value, name);

                    //평정자
                    range = Util.OSheet.get_Range("C" + rowIndex.ToString(), Missing.Value);
                    range.set_Value(Missing.Value, "평정자");

                    //근무성적평정정보(평정자)
                    ArrayList evaluePoint = DBUtil.getWorkEvalueInfo("0", tmp[1].ToString());
                    if (evaluePoint.Count == 0)
                    {
                        lblMsg.Text = "평정자 및 확인자 모두 평정 하신 후 인쇄하십시오.";
                        btnPrint.Enabled = true;
                        Util.closeXls();
                        return;
                    }

                    //교육자로서의 품성
                    range = Util.OSheet.get_Range("D" + rowIndex.ToString(), Missing.Value);
                    range.set_Value(Missing.Value, evaluePoint[0]);

                    //공직자로서의 자세
                    range = Util.OSheet.get_Range("E" + rowIndex.ToString(), Missing.Value);
                    range.set_Value(Missing.Value, evaluePoint[1]);

                    //학습지도
                    range = Util.OSheet.get_Range("F" + rowIndex.ToString(), Missing.Value);
                    range.set_Value(Missing.Value, evaluePoint[2]);

                    //생활지도
                    range = Util.OSheet.get_Range("G" + rowIndex.ToString(), Missing.Value);
                    range.set_Value(Missing.Value, evaluePoint[3]);

                    //교육연구 및 담당업무
                    range = Util.OSheet.get_Range("H" + rowIndex.ToString(), Missing.Value);
                    range.set_Value(Missing.Value, evaluePoint[4]);

                    //평정점
                    range = Util.OSheet.get_Range("I" + rowIndex.ToString(), Missing.Value);
                    range.set_Value(Missing.Value, evaluePoint[5]);

                    //환산점
                    range = Util.OSheet.get_Range("J" + rowIndex.ToString(), Missing.Value);
                    range.set_Value(Missing.Value, evaluePoint[6]);

                    //총점에 합산
                    total += Double.Parse(evaluePoint[6].ToString());

                    //확인자
                    range = Util.OSheet.get_Range("C" + (rowIndex + 1).ToString(), Missing.Value);
                    range.set_Value(Missing.Value, "확인자");

                    //근무성적평정정보(확인자)
                    evaluePoint = DBUtil.getWorkEvalueInfo("1", tmp[1].ToString());
                    if (evaluePoint.Count == 0)
                    {
                        lblMsg.Text = "평정자 및 확인자 모두 평정 하신 후 인쇄하십시오.";
                        btnPrint.Enabled = true;
                        Util.closeXls();
                        return;
                    }

                    //교육자로서의 품성
                    range = Util.OSheet.get_Range("D" + (rowIndex + 1).ToString(), Missing.Value);
                    range.set_Value(Missing.Value, evaluePoint[0]);

                    //공직자로서의 자세
                    range = Util.OSheet.get_Range("E" + (rowIndex + 1).ToString(), Missing.Value);
                    range.set_Value(Missing.Value, evaluePoint[1]);

                    //학습지도
                    range = Util.OSheet.get_Range("F" + (rowIndex + 1).ToString(), Missing.Value);
                    range.set_Value(Missing.Value, evaluePoint[2]);

                    //생활지도
                    range = Util.OSheet.get_Range("G" + (rowIndex + 1).ToString(), Missing.Value);
                    range.set_Value(Missing.Value, evaluePoint[3]);

                    //교육연구 및 담당업무
                    range = Util.OSheet.get_Range("H" + (rowIndex + 1).ToString(), Missing.Value);
                    range.set_Value(Missing.Value, evaluePoint[4]);

                    //평정점
                    range = Util.OSheet.get_Range("I" + (rowIndex + 1).ToString(), Missing.Value);
                    range.set_Value(Missing.Value, evaluePoint[5]);

                    //환산점
                    range = Util.OSheet.get_Range("J" + (rowIndex + 1).ToString(), Missing.Value);
                    range.set_Value(Missing.Value, evaluePoint[6]);

                    //총점에 합산
                    total += Double.Parse(evaluePoint[6].ToString());

                    //총점
                    range = Util.OSheet.get_Range("K" + rowIndex.ToString(), Missing.Value);
                    range.set_Value(Missing.Value, total.ToString("#0.000"));

                    rowIndex += 2;

                    lblMsg.Text = DBUtil.getMTargetName(tmp[1].ToString()) + " 데이터 입력 중...";
                }

                lblMsg.Text = "서식 적용 중...";
                range = Util.OSheet.get_Range("A16", "K" + (rowIndex - 1).ToString());
                range.Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeBottom].Weight = Excel.XlBorderWeight.xlThin;
                range.Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeLeft].Weight = Excel.XlBorderWeight.xlThin;
                range.Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeRight].Weight = Excel.XlBorderWeight.xlThin;
                range.Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlInsideHorizontal].Weight = Excel.XlBorderWeight.xlThin;
                range.Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlInsideVertical].Weight = Excel.XlBorderWeight.xlThin;
                range = Util.OSheet.get_Range("A" + rowIndex + 2, "K1000");
                range.Delete(Excel.XlDeleteShiftDirection.xlShiftUp);

                string myDocPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
                string fileName = myDocPath + "\\tmp.xls";
                FileInfo f = new FileInfo(fileName);
                if (f.Exists)
                {
                    f.Delete();
                }

                Util.OWorkBook.SaveAs(fileName, Missing.Value, Config.xls_password, Missing.Value, true, Missing.Value, Excel.XlSaveAsAccessMode.xlShared, Excel.XlSaveConflictResolution.xlLocalSessionChanges, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
                Util.OExcel.Visible = true;
                Util.OWorkBook.PrintPreview(Missing.Value);

                f.Delete();
                //lblMsg.Text = "인쇄합니다...";
                //PrintDialog dlg = new PrintDialog();
                //dlg.AllowSomePages = true;
                //dlg.AllowCurrentPage = true;
                //dlg.AllowPrintToFile = false;
                //dlg.PrinterSettings.Duplex = System.Drawing.Printing.Duplex.Vertical;

                //if (dlg.ShowDialog() == DialogResult.OK)
                //{
                //    int startPage = dlg.PrinterSettings.FromPage;
                //    int endPage = dlg.PrinterSettings.ToPage;
                //    Util.OSheet.PageSetup.CenterHorizontally = true;

                //    range = Util.OSheet.get_Range("A1", "K" + (rowIndex - 1).ToString());
                //    if (startPage > 0 && endPage > 0)
                //    {
                //        range.PrintOut(startPage, endPage, dlg.PrinterSettings.Copies,false, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
                //    }
                //    else
                //    {                     
                //        range.PrintOut(Missing.Value, Missing.Value, dlg.PrinterSettings.Copies, false, dlg.PrinterSettings.PrinterName, Missing.Value, dlg.PrinterSettings.Collate, Missing.Value);

                //    }
                //}                
                //dlg.Dispose();

                Util.closeXls();
                lblMsg.Text = "";
                btnPrint.Enabled = true;

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + "\n\n" + ex.StackTrace, "오류");
                Util.closeXls();
            }
        }

        private void printXlsFinal()
        {
            try
            {
                btnPrint.Enabled = false;
                lblMsg.Text = "인쇄 준비 중...";
                Util.openXls(Application.StartupPath + "\\WorkEvalueFinal");

                //평정기간설정
                Excel.Range range = Util.OSheet.get_Range("A3", Missing.Value);
                range.set_Value(Missing.Value, Config.Mvi.SDate);

                range = Util.OSheet.get_Range("A4", Missing.Value);
                range.set_Value(Missing.Value, Config.Mvi.EDate);

                //확인자
                range = Util.OSheet.get_Range("M3", Missing.Value);
                range.set_Value(Missing.Value, "교장");

                range = Util.OSheet.get_Range("M4", Missing.Value);
                range.set_Value(Missing.Value, Config.Mvi.Confirmer);

                //평정자
                range = Util.OSheet.get_Range("P3", Missing.Value);
                range.set_Value(Missing.Value, "교감");

                range = Util.OSheet.get_Range("P4", Missing.Value);
                range.set_Value(Missing.Value, Config.Mvi.Evaluer1 + "," + Config.Mvi.Evaluer2);

                lblMsg.Text = "데이터 입력 중...";
                //근무성적평정대상자 목록
                ArrayList list = DBUtil.getMTargetArrayFromWorkControlTable(getTargetType());
                int rowIndex = 13;//2행씩 증가
                for (int i = 0; i < list.Count; i++)
                {
                    ArrayList tmp = (ArrayList)list[i];

                    //소속
                    range = Util.OSheet.get_Range("A" + (rowIndex).ToString(), Missing.Value);
                    range.set_Value(Missing.Value, DBUtil.getOrganName(Config.Mvi.Organ));

                    //성명
                    range = Util.OSheet.get_Range("D" + (rowIndex).ToString(), Missing.Value);
                    string name = DBUtil.getMTargetName(tmp[1].ToString());
                    if (DBUtil.getSameNameExist(name)) name = name + "(" + DBUtil.getMTargetInfo(tmp[1].ToString())[2].ToString() + ")";
                    range.set_Value(Missing.Value, name);

                    //평정자
                    range = Util.OSheet.get_Range("E" + rowIndex.ToString(), Missing.Value);
                    range.set_Value(Missing.Value, "평정자");

                    //근무성적평정정보(평정자)
                    ArrayList evaluePoint = DBUtil.getWorkEvalueInfo("0", tmp[1].ToString());
                    if (evaluePoint.Count == 0)
                    {
                        lblMsg.Text = "평정자 및 확인자 모두 평정 하신 후 인쇄하십시오.";
                        btnPrint.Enabled = true;
                        Util.closeXls();
                        return;
                    }
                    //교육자로서의 품성
                    range = Util.OSheet.get_Range("F" + rowIndex.ToString(), Missing.Value);
                    range.set_Value(Missing.Value, evaluePoint[0]);

                    //공직자로서의 자세
                    range = Util.OSheet.get_Range("G" + rowIndex.ToString(), Missing.Value);
                    range.set_Value(Missing.Value, evaluePoint[1]);

                    //학습지도
                    range = Util.OSheet.get_Range("H" + rowIndex.ToString(), Missing.Value);
                    range.set_Value(Missing.Value, evaluePoint[2]);

                    //생활지도
                    range = Util.OSheet.get_Range("I" + rowIndex.ToString(), Missing.Value);
                    range.set_Value(Missing.Value, evaluePoint[3]);

                    //교육연구 및 담당업무
                    range = Util.OSheet.get_Range("J" + rowIndex.ToString(), Missing.Value);
                    range.set_Value(Missing.Value, evaluePoint[4]);

                    //평정점
                    range = Util.OSheet.get_Range("K" + rowIndex.ToString(), Missing.Value);
                    range.set_Value(Missing.Value, evaluePoint[5]);

                    //환산점
                    range = Util.OSheet.get_Range("M" + rowIndex.ToString(), Missing.Value);
                    range.set_Value(Missing.Value, evaluePoint[6]);

                    //확인자
                    range = Util.OSheet.get_Range("E" + (rowIndex + 1).ToString(), Missing.Value);
                    range.set_Value(Missing.Value, "확인자");

                    //근무성적평정정보(확인자)
                    evaluePoint = DBUtil.getWorkEvalueInfo("1", tmp[1].ToString());
                    if (evaluePoint.Count == 0)
                    {
                        lblMsg.Text = "평정자 및 확인자 모두 평정 하신 후 인쇄하십시오.";
                        btnPrint.Enabled = true;
                        Util.closeXls();
                        return;
                    }
                    //교육자로서의 품성
                    range = Util.OSheet.get_Range("F" + (rowIndex + 1).ToString(), Missing.Value);
                    range.set_Value(Missing.Value, evaluePoint[0]);

                    //공직자로서의 자세
                    range = Util.OSheet.get_Range("G" + (rowIndex + 1).ToString(), Missing.Value);
                    range.set_Value(Missing.Value, evaluePoint[1]);

                    //학습지도
                    range = Util.OSheet.get_Range("H" + (rowIndex + 1).ToString(), Missing.Value);
                    range.set_Value(Missing.Value, evaluePoint[2]);

                    //생활지도
                    range = Util.OSheet.get_Range("I" + (rowIndex + 1).ToString(), Missing.Value);
                    range.set_Value(Missing.Value, evaluePoint[3]);

                    //교육연구 및 담당업무
                    range = Util.OSheet.get_Range("J" + (rowIndex + 1).ToString(), Missing.Value);
                    range.set_Value(Missing.Value, evaluePoint[4]);

                    //평정점
                    range = Util.OSheet.get_Range("K" + (rowIndex + 1).ToString(), Missing.Value);
                    range.set_Value(Missing.Value, evaluePoint[5]);

                    //환산점
                    range = Util.OSheet.get_Range("M" + (rowIndex + 1).ToString(), Missing.Value);
                    range.set_Value(Missing.Value, evaluePoint[6]);

                    //조정점 정보
                    ArrayList controlPoint = DBUtil.getWorkControlInfo(tmp[1].ToString());

                    //총점
                    range = Util.OSheet.get_Range("N" + rowIndex.ToString(), Missing.Value);
                    range.set_Value(Missing.Value, controlPoint[0]);

                    //조정점
                    range = Util.OSheet.get_Range("P" + rowIndex.ToString(), Missing.Value);
                    range.set_Value(Missing.Value, controlPoint[1]);

                    //순위
                    range = Util.OSheet.get_Range("Q" + rowIndex.ToString(), Missing.Value);
                    range.set_Value(Missing.Value, controlPoint[2]);

                    rowIndex += 2;

                    lblMsg.Text = DBUtil.getMTargetName(tmp[1].ToString()) + " 데이터 입력 중...";

                }
                lblMsg.Text = "서식 적용 중...";
                range = Util.OSheet.get_Range("A13", "Q" + (rowIndex - 1).ToString());
                range.Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeBottom].Weight = Excel.XlBorderWeight.xlThin;
                range.Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeLeft].Weight = Excel.XlBorderWeight.xlThin;
                range.Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeRight].Weight = Excel.XlBorderWeight.xlThin;
                range.Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlInsideHorizontal].Weight = Excel.XlBorderWeight.xlThin;
                range.Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlInsideVertical].Weight = Excel.XlBorderWeight.xlThin;
                range = Util.OSheet.get_Range("A" + rowIndex, "O1000");
                range.Delete(Excel.XlDeleteShiftDirection.xlShiftUp);

                string myDocPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
                string fileName = myDocPath + "\\tmp.xls";
                FileInfo f = new FileInfo(fileName);
                if (f.Exists)
                {
                    f.Delete();
                }

                Util.OWorkBook.SaveAs(fileName, Missing.Value, Config.xls_password, Missing.Value, true, Missing.Value, Excel.XlSaveAsAccessMode.xlShared, Excel.XlSaveConflictResolution.xlLocalSessionChanges, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
                Util.OExcel.Visible = true;
                Util.OWorkBook.PrintPreview(Missing.Value);

                f.Delete();
                //lblMsg.Text = "인쇄합니다...";
                //PrintDialog dlg = new PrintDialog();
                //dlg.AllowSomePages = true;
                //dlg.AllowCurrentPage = true;
                //dlg.AllowPrintToFile = false;
                //dlg.PrinterSettings.Duplex = System.Drawing.Printing.Duplex.Vertical;
                //if (dlg.ShowDialog() == DialogResult.OK)
                //{
                //    int startPage = dlg.PrinterSettings.FromPage;
                //    int endPage = dlg.PrinterSettings.ToPage;
                //    Util.OSheet.PageSetup.CenterHorizontally = true;

                //    range = Util.OSheet.get_Range("A1", "Q" + (rowIndex - 1).ToString());
                //    if (startPage > 0 && endPage > 0)
                //    {
                //        range.PrintOut(startPage, endPage, dlg.PrinterSettings.Copies, false, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
                //    }
                //    else
                //    {
                //        range.PrintOut(Missing.Value, Missing.Value, dlg.PrinterSettings.Copies, false, dlg.PrinterSettings.PrinterName, Missing.Value, dlg.PrinterSettings.Collate, Missing.Value);
                //    }
                //}
                //dlg.Dispose();

                Util.closeXls();
                lblMsg.Text = "";
                btnPrint.Enabled = true;

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + "\n\n" + ex.StackTrace, "오류");
                Util.closeXls();
            }
        }

        private byte getTargetType()
        {
            return (byte)parent.tscTargetType.SelectedIndex;
        }

        private void grdEvalue_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            int rowIndex = e.RowIndex;
            int colIndex = e.ColumnIndex;

            double[] maxPoint = { 10.0, 10.0, 40.0, 20.0, 20.0 };
            double[] curPoint = new double[5];
            double total_point = 0.0;

            if ((int)getTargetType() > 0)
            {
                if ((double)grdEvalue.Rows[rowIndex].Cells[colIndex].Value > maxPoint[colIndex - 6])
                {
                    MessageBox.Show("해당 항목의 최대값은 " + maxPoint[colIndex - 6].ToString() + " 점 입니다.   ", "확인", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    grdEvalue.Rows[rowIndex].Cells[colIndex].Value = 0.0;
                    return;
                }

                curPoint[0] = (double)grdEvalue.Rows[rowIndex].Cells[6].Value;
                curPoint[1] = (double)grdEvalue.Rows[rowIndex].Cells[7].Value;
                curPoint[2] = (double)grdEvalue.Rows[rowIndex].Cells[8].Value;
                curPoint[3] = (double)grdEvalue.Rows[rowIndex].Cells[9].Value;
                curPoint[4] = (double)grdEvalue.Rows[rowIndex].Cells[10].Value;

                total_point = curPoint[0] + curPoint[1] + curPoint[2] + curPoint[3] + curPoint[4];
                grdEvalue.Rows[rowIndex].Cells[11].Value = total_point;
                double ratio = 0.0;
                if (cboManager.SelectedIndex == 0)
                {
                    ratio = 0.3;
                }
                else
                {
                    ratio = 0.4;
                }
                grdEvalue.Rows[rowIndex].Cells[12].Value = total_point * ratio;
            }

        }
    }
}
